package com.uxbee.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uxbee.pojo.HtmlData;
import com.uxbee.service.GertWebInfo;

public class GetData extends HttpServlet {

	/**
	 * Create date:2012-9-22 Describe :TODO
	 */
	private static final long serialVersionUID = 8392589105973932215L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String[] urlArray = request.getParameterValues("webUrl");
		List<HtmlData> list = null;
		if (urlArray != null && urlArray.length > 0) {
			list = new ArrayList<HtmlData>();
			for (int i = 0; i < urlArray.length; i++) {
				// 获取URL
				String url = urlArray[i];
				// 获取主域名
				String mainUrl = urlArray[i].replaceAll("^.*[.]", "");
				int s = url.indexOf("//");
				int e = url.indexOf("/", s + 2);
				mainUrl = url.substring(s + 2, e);
				e = mainUrl.lastIndexOf(".");
				s = mainUrl.lastIndexOf(".", e - 1);
				mainUrl = mainUrl.substring(s + 1);
				// 设置查询参数
				HtmlData htmlData = new HtmlData();
				htmlData.setUrl(url);
				htmlData.setMainUrl(mainUrl);
				htmlData.setLevel(1);
				list.add(htmlData);
			}
		}
		// 设置初始化数据
		GertWebInfo.setInitData(list);
	}
}
